package com.myjoje.dao.system;

import com.myjoje.base.BaseDao;
import com.myjoje.base.BaseService;
import com.myjoje.bean.UserBean;
import com.myjoje.bean.system.RoleBean;
import com.myjoje.config.AppCache;
import com.myjoje.config.annotation.MapperConfig;
import com.myjoje.config.support.DaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@MapperConfig(mapperName = "UserMapper", tableName = "sy_user")
public class UserDao extends BaseDao {

    @Autowired
    private DaoSupport daoSupport;

    /**
     * 根据身份获取用户信息，不查询已删除用户
     */
    public UserBean findUserByPrincipal(String principal) {
        Map map = getParams("principal", principal);
        return (UserBean) daoSupport.selectForObject(getStatement("findUserByPrincipal"), map);
    }

    public List<RoleBean> getRoleList(Integer currentPage, Integer pageSize, Map params) {
        Map map = getParams(currentPage, pageSize, params);
        return (List<RoleBean>) daoSupport.selectForList(getStatement("getRoleList"), map);
    }

    public Long getRoleCount(Map params) {
        return (Long) daoSupport.selectForObject(getStatement("getRoleCount"), params);
    }

    //更新root角色的部门id
    public void updateRootDeptIds(String deptIds) {
        Map map = getParams("deptIds", deptIds);
        map.put("role", AppCache.getRoleRoot());
        daoSupport.update(getStatement("updateRootDeptIds"), map);
    }

}
